<template>
  <div>
    <commonBox tow="角色管理" one="角色列表"></commonBox>
    <el-card>
      <el-button type="primary"> 添加角色</el-button>
    </el-card>
    <!-- 列表表格 -->
    <el-table :data="rowlist" style="width: 100%" stripe border>
      <!-- 展开行 -->
      <el-table-column type="expand">
        <!-- 展开行中模板 -->
        <template slot-scope="scope">
          <!-- 每一行 -->
          <el-row
            v-for="(item1, index1) in scope.row.children"
            :key="index1"
            class="el-rows"
            :class="index1 == 0 ? 'eltop' : ''"
          >
            <!-- 每一列的分数 ,第一行-->
            <el-col :span="5">
              <el-tag closable type="primary">
                {{ item1.authName }}
              </el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <!-- 第二列 -->
            <el-col :span="19">
              <!-- 第二列中一行 -->
              <el-row
                v-for="(item2, index2) in item1.children"
                :key="index2"
                class="two-row"
                :class="index2 == 0 ? '' : 'eltop'"
              >
                <!-- 善于利用三元表达式，理清逻辑如何实现 -->
                <el-col :span="6">
                  <el-tag closable type="warning">
                    {{ item2.authName }}
                  </el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col>
                  <el-tag
                    type="danger"
                    closable
                    :span="18"
                    v-for="(item3, index3) in item2.children"
                    :key="index3"
                    @close="handclose(item3.id, scope.row)"
                  >
                    {{ item3.authName }}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>

      <el-table-column label="#" type="index"> </el-table-column>
      <el-table-column prop="roleName" label="用户"> </el-table-column>
      <el-table-column prop="roleDesc" label="角色"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >修改</el-button
          >
          <el-button type="warning" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-share"
            size="mini"
            @click="pops(scope.row)"
            >分配权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分配权限弹框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="pop"
      width="50%"
      @close="losebox"
    >
      <span>
        <!-- 树形控件 -->
        <el-tree
          :data="treelist"
          :props="defaultProps"
          :default-expand-all="true"
          :default-checked-keys="defaluteOpt"
          node-key="id"
          show-checkbox
          ref="treeref"
        ></el-tree>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="pop = false">取 消</el-button>
        <el-button type="primary" @click="authority">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 树形控件唯一id
      id: "",

      // 默认选中数组
      defaluteOpt: [],
      // 树形控件数据
      treelist: [],
      // 分配权限弹出框事件pop
      pop: false,
      //    列表数据
      rowlist: [],
      // 树形控件节点
      defaultProps: {
        children: "children",
        label: "authName",
      },
    };
  },
  created() {
    this.query();
  },
  methods: {
    async query() {
      const { data: res } = await this.$http.get("roles");
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取列表失败");
      }
      this.$message.success("获取列表成功");
      this.rowlist = res.data;
    },
    // 关闭标签
    handclose(id1, role) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //    console.log(id1);
          const { data: res } = await this.$http.delete(
            "roles/" + role.id + "/rights/" + id1
          );
          console.log(res, "///");
          if (res.meta.status !== 200) {
            return this.$message.error("删除失败");
          }
          role.children = res.data;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async pops(role) {
      const { data: res } = await this.$http.get("rights/tree");
      //  console.log(res,'...');
      if (res.meta.status !== 200) {
        return this.$message.error("错误");
      }
      this.treelist = res.data;
      this.id = role.id;
      // 直接调用递归函数
      this.recurev(role, this.defaluteOpt);
      this.pop = true;
    },
    // 遍历递归函数
    recurev(node, arr) {
      // 三级菜单没有children
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => {
        this.recurev(item, this.defaluteOpt);
      });
    },
    losebox() {
      this.defaluteOpt = [];
    },
    // 分配权限
    async authority() {
      const keys = this.$refs.treeref.getCheckedKeys();
      const changs = this.$refs.treeref.getHalfCheckedKeys();
      const authid = [...keys, ...changs];
      const authids = authid.join(",");
      const { data: res } = await this.$http.post(
        "roles/" + this.id + "/rights",
        { rids: authids }
      );
      if (res.meta.status !== 200) {
        return this.$message.error("分配权限失败");
      }
      this.$message.success("分配权限成功");
      this.query()
      this.pop = false;
      
    },
  },
};
</script>

<style lang="less" scoped>
.el-tag {
  margin: 20px;
}
.eltop {
  border-top: 1px solid #eee;
}
.el-rows {
  border-bottom: 1px solid #eee;
}
// 善于利用三元表达式，理清逻辑如何实现
// .two-row{
//     border-top: 1px solid #eee;
// }
.el-row {
  display: flex;
  align-items: center;
}
</style>